package com.taotao.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.taotao.common.service.ApiService;
import com.taotao.common.utils.EasyUIResult;
import com.taotao.manage.pojo.Content;

@Service
public class IndexService {
	
	//通过value进行注入
	@Value("${TAOTAO_MANAGE_URL}")
	private String TAOTAO_MANAGE_URL;
	
	@Value("${INDEX_AD1_URL}")
	private String INDEX_AD1_URL;
	
	@Autowired
	private ApiService apiService;
	
	//解析json数据
	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	/**
	 * 通过调用后台系统提供接口获取数据
	 * 方法1：
	 * @return
	 */
//	public String getIndexAd1() {
//		String url=TAOTAO_MANAGE_URL+INDEX_AD1_URL;
//		try {
//			String jsonData =apiService.doGet(url);
//			//解析jsonData，获取需要的数据
//			//封装成前端所需的格式
//			JsonNode jsonNode = MAPPER.readTree(jsonData);
//			ArrayNode arrayNode =(ArrayNode) jsonNode.get("rows");
//			//定义List
//			List<Map<String, Object>> result = new ArrayList<>();
//			for (JsonNode node : arrayNode) {
//				Map<String, Object> map = new LinkedHashMap<>();
//				map.put("srcB", node.get("pic").asText());
//				map.put("height", 240);
//				map.put("alt", node.get("title").asText());
//				map.put("width", 670);
//				map.put("src", map.get("srcB"));
//				map.put("widthB", 550);
//				map.put("href", node.get("url").asText());
//				map.put("heightB", 240);
//				result.add(map);
//			}
//			//将java对象序列化成json数据
//			System.out.println("****************************************");
//			System.out.println(MAPPER.writeValueAsString(result));
//			return MAPPER.writeValueAsString(result);
//			
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	
	/**
	 * 通过调用后台系统提供接口获取数据
	 * 方法2：用easyuUIresult
	 * @return
	 */
	public String getIndexAd1() {
		String url=TAOTAO_MANAGE_URL+INDEX_AD1_URL;
		try {
			String jsonData =apiService.doGet(url);
			EasyUIResult easyUIResult = EasyUIResult.formatToList(jsonData, Content.class);
			List<Content> list = (List<Content>) easyUIResult.getRows();
			//定义List
			List<Map<String, Object>> result = new ArrayList<>();
			for (Content content : list) {
				Map<String, Object> map = new LinkedHashMap<>();
				map.put("srcB", content.getPic());
				map.put("height", 240);
				map.put("alt", content.getTitle());
				map.put("width", 670);
				map.put("src", content.getPic());
				map.put("widthB", 550);
				map.put("href",content.getUrl());
				map.put("heightB", 240);
				result.add(map);
			}
			//将java对象序列化成json数据
			//System.out.println("****************************************");
			//System.out.println(MAPPER.writeValueAsString(result));
			return MAPPER.writeValueAsString(result);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
